<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" /> 
    <title> - 天地维杰网</title>
    <meta name="keywords" content="系统架构,shutdown,不与天斗,Domino,博客,程序员,架构师,笔记,技术,分享,java,Redis">
    
    <meta property="og:title" content="">
    <meta property="og:site_name" content="天地维杰网">
    <meta property="og:image" content="/img/author.jpg"> 
    <meta name="title" content=" - 天地维杰网" />
    <meta name="description" content="天地维杰网 | 博客 | 软件 | 架构 | Java "> 
    <link rel="shortcut icon" href="http://www.shutdown.cn/img/favicon.ico" />
    <link rel="apple-touch-icon" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link rel="apple-touch-icon-precomposed" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link href="http://www.shutdown.cn/js/vendor/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/main.css" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/syntax.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post"},
     fancybox: true, 
    motion: true
  };
</script>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-7826003325059020" crossorigin="anonymous"></script>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<div class="container one-collumn sidebar-position-left page-home  ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"> <div class="site-meta  custom-logo ">

  <div class="custom-logo-site-title">
    <a href="http://www.shutdown.cn"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">天地维杰网</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">人如秋鸿来有信，事若春梦了无痕</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
    <ul id="menu" class="menu">
      
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/redis/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-full"></i> <br />Redis
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/java/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-coffee"></i> <br />java
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/linux/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-linux"></i> <br />linux
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/daily/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bug"></i> <br />日常问题
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/spring/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-child"></i> <br />Spring和Springboot
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/mac/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-fire"></i> <br />Mac相关
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/middleware/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-gavel"></i> <br />中间件
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jiagou/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-rocket"></i> <br />架构
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/python/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-ship"></i> <br />python
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/front/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bolt"></i> <br />前端
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jvm/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-balance-scale"></i> <br />jvm
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/c/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-empty"></i> <br />c语言
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/web3/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-web3"></i> <br />web3
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/post/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />归档
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/about/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />关于
          </a>
        </li>
      
      <li class="menu-item menu-item-search">
        <a href="javascript:;" class="popup-trigger"> <i class="menu-item-icon fa fa-search fa-fw"></i> <br /> 搜索</a>
      </li>
    </ul>
    <div class="site-search">
      <div class="popup">
 <span class="search-icon fa fa-search"></span>
 <input type="text" id="local-search-input">
 <div id="local-search-result"></div>
 <span class="popup-btn-close">close</span>
</div>

    </div>
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
<section id="posts" class="posts-expand">
  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <header class="post-header">
      <h1 class="post-title" itemprop="name headline">
        <a class="post-title-link" href="http://www.shutdown.cn/post/redis6.0%E6%96%B0%E7%89%B9%E6%80%A7-%E5%AE%A2%E6%88%B7%E7%AB%AF%E7%BC%93%E5%AD%98%E5%85%B6%E4%BA%8C/" itemprop="url">
        
        </a>
      </h1>
      <div class="post-meta">
      <span class="post-time">
<span class="post-meta-item-icon">
    <i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">时间：</span>
<time itemprop="dateCreated" datetime="2016-03-22T13:04:35+08:00" content="0001-01-01">
    0001-01-01
</time>
</span> 
      
      
       <span>
&nbsp; | &nbsp;
<span class="post-meta-item-icon">
    <i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">阅读：</span>
<span class="leancloud-visitors-count">326 字 ~2分钟</span>
</span>
      </div>
    </header>
    <div class="post-body" itemprop="articleBody">
    

    

<blockquote>
<p>原文地址：<a href="https://www.cnblogs.com/binecy/p/15399578.html">https://www.cnblogs.com/binecy/p/15399578.html</a></p>
</blockquote>

<p>本文探究Redis最新特性&ndash;客户端缓存在SpringBoot上的应用实战。</p>

<h2 id="redis-tracking">Redis Tracking</h2>

<p>Redis客户端缓存机制基于Redis Tracking机制实现的。我们先了解一下Redis Tracking机制。</p>

<h3 id="为什么需要redis-tracking">为什么需要Redis Tracking</h3>

<p>Redis由于速度快、性能高，常常作为MySQL等传统数据库的缓存数据库。但由于Redis是远程服务，查询Redis需要通过网络请求，在高并发查询情景中难免造成性能损耗。所以，高并发应用通常引入本地缓存，在查询Redis前先检查本地缓存是否存在数据。
假如使用MySQL存储数据，那么数据查询流程下图所示。</p>

<div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/20220628172017.png" width = 200 /> </div>

<p>引入多端缓存后，修改数据时，各数据缓存端如何保证数据一致是一个难题。通常的做法是修改MySQL数据，并删除Redis缓存、本地缓存。当用户发现缓存不存在时，会重新查询MySQL数据，并设置Redis缓存、本地缓存。
在分布式系统中，某个节点修改数据后不仅要删除当前节点的本地缓存，还需要发送请求给集群中的其他节点，要求它们删除该数据的本地缓存，如下图所示。如果分布式系统中节点很多，那么该操作会造成不少性能损耗。</p>

<div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/20220628172059.png" width = 600 /> </div>

<p>为此，Redis 6提供了Redis Tracking机制，对该缓存方案进行了优化。开启Redis Tracking后，Redis服务器会记录客户端查询的所有键，并在这些键发生变更后，发送失效消息通知客户端这些键已变更，这时客户端需要将这些键的本地缓存删除。基于Redis Tracking机制，某个节点修改数据后，不需要再在集群广播“删除本地缓存”的请求，从而降低了系统复杂度，并提高了性能。</p>

<h3 id="redis-tracking的应用">Redis Tracking的应用</h3>

<p>下表展示了Redis Tracking的基本使用</p>

<div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/20220628172224.png" width = 600 /> </div>

<p>（1）为了支持Redis服务器推送消息，Redis在RESP2协议上进行了扩展，实现了RESP3协议。HELLO 3命令表示客户端与Redis服务器之间使用RESP3协议通信。
注意：Redis 6.0提供了Redis Tracking机制，但该版本的redis-cli并不支持RESP3协议，所以这里需要使用Redis 6.2版本的redis-cli进行演示。
（2）CLIENT TRACKING on命令的作用是开启Redis Tracking机制，此后Redis服务器会记录客户端查询的键，并在这些键变更后推送失效消息通知客户端。失效消息以invalidate开头，后面是失效键数组。
上表中的客户端 client1 查询了键 score 后，客户端 client2 修改了该键，这时 Redis 服务器会马上推送失效消息给客户端 client1，但 redis-cli 不会直接展示它收到的推送消息，而是在下一个请求返回后再展示该消息，所以 client1 重新发送了一个 PING请求。</p>

<p>上面使用的非广播模式，另外，Redis Tracking还支持广播模式。在广播模式下，当变更的键以客户端关注的前缀开头时，Redis服务器会给所有关注了该前缀的客户端发送失效消息，不管客户端之前是否查询过这些键。
下表展示了如何使用Redis Tracking的广播模式。</p>

<div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/20220628172310.png" width = 600 /> </div>

<p>说明一下CLIENT TRACKING命令中的两个参数：
BCAST参数：启用广播模式。
PREFIX参数：声明客户端关注的前缀，即客户端只关注cache开头的键。</p>

<p>强调一下非广播模式与广播模式的区别：
非广播模式：Redis服务器记录客户查询过的键，当这些键发生变化时，Redis发送失效消息给客户端。
广播模式：Redis服务器不记录客户查询过的键，当变更的键以客户端关注的前缀开头时，Redis就会发送失效消息给客户端。</p>

<p>关于Redis Tracking的更多内容，我已经在新书《Redis核心原理与实践》中详细分析，这里不再赘述。</p>

<h2 id="redis客户端缓存">Redis客户端缓存</h2>

<p>既然Redis提供了Tracking机制，那么客户端就可以基于该机制实现客户端缓存了。</p>

<h3 id="lettuce实现">Lettuce实现</h3>

<p>Lettuce（6.1.5版本）已经支持Redis客户端缓存（单机模式下），使用CacheFrontend类可以实现客户端缓存。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-typescript" data-lang="typescript"><span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">static</span> <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">main</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87">String</span><span style="color:#000;font-weight:bold">[</span><span style="color:#000;font-weight:bold">]</span> <span style="color:#000">args</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">throws</span> <span style="color:#000">InterruptedException</span> <span style="color:#000;font-weight:bold">{</span>
    <span style="color:#8f5902;font-style:italic">// [1]
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#000">RedisURI</span> <span style="color:#000">redisUri</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">RedisURI</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">builder</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span>
            <span style="color:#000;font-weight:bold">.</span><span style="color:#000">withHost</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;127.0.0.1&#34;</span><span style="color:#000;font-weight:bold">)</span>
            <span style="color:#000;font-weight:bold">.</span><span style="color:#000">withPort</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">6379</span><span style="color:#000;font-weight:bold">)</span>
            <span style="color:#000;font-weight:bold">.</span><span style="color:#000">build</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000">RedisClient</span> <span style="color:#000">redisClient</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">RedisClient</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">create</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">redisUri</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>

    <span style="color:#8f5902;font-style:italic">// [2]
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#000">StatefulRedisConnection</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#204a87">String</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87">String</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">connect</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">redisClient</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">connect</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000">Map</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#204a87">String</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87">String</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">clientCache</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#000">ConcurrentHashMap</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000">CacheFrontend</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#204a87">String</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87">String</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">frontend</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">ClientSideCaching</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">enable</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">CacheAccessor</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">forMap</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">clientCache</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">connect</span><span style="color:#000;font-weight:bold">,</span>
            <span style="color:#000">TrackingArgs</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Builder</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">enabled</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>

    <span style="color:#8f5902;font-style:italic">// [3]
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#204a87;font-weight:bold">while</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">true</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
        <span style="color:#204a87">String</span> <span style="color:#000">cachedValue</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">frontend</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">get</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;k1&#34;</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000">System</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">out</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">println</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;k1 ---&gt; &#34;</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#000">cachedValue</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000">Thread</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">sleep</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">3000</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000;font-weight:bold">}</span>
<span style="color:#000;font-weight:bold">}</span>
</code></pre></div>
<ol>
<li>构建RedisClient。</li>
<li>构建CacheFrontend。
ClientSideCaching.enable开启客户端缓存，即发送“CLIENT TRACKING”命令给Redis服务器，要求Redis开启Tracking机制。
最后一个参数指定了Redis Tracking的模式，这里用的是最简单的非广播模式。
这里可以看到，通过Map保存客户端缓存的内容。</li>
<li>重复查询同一个值，查看缓存是否生效。</li>
</ol>

<p>我们可以通过Redis的Monitor命令监控Redis服务收到的命令，使用该命令就可以看到，开启客户端缓存后，Lettuce不会重复查询同一个键。
而且我们修改这个键后，Lettuce会重新查询这个键的最新值。</p>

<p>通过Redis的Client List命令可以查看连接的信息</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-objectivec" data-lang="objectivec"><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">CLIENT</span> <span style="color:#000">LIST</span>
<span style="color:#204a87;font-weight:bold">id</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">4</span> <span style="color:#000">addr</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">192.168</span><span style="color:#0000cf;font-weight:bold">.56</span><span style="color:#0000cf;font-weight:bold">.1</span><span style="color:#ce5c00;font-weight:bold">:</span><span style="color:#0000cf;font-weight:bold">50402</span> <span style="color:#000">fd</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">7</span> <span style="color:#000">name</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">age</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">23</span> <span style="color:#000">idle</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">22</span> <span style="color:#000">flags</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#000">t</span> <span style="color:#000;font-weight:bold">.</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000;font-weight:bold">.</span></code></pre></div>
<p><code>flags=t</code>代表这个连接启动了Tracking机制。</p>

<h3 id="springboot应用">SpringBoot应用</h3>

<p>那么如何在SpringBoot上使用呢？请看下面的例子</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-typescript" data-lang="typescript"><span style="color:#204a87;font-weight:bold">@Bean</span>
<span style="color:#204a87;font-weight:bold">public</span> <span style="color:#000">CacheFrontend</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#204a87">String</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87">String</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">redisCacheFrontend</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">RedisConnectionFactory</span> <span style="color:#000">redisConnectionFactory</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
    <span style="color:#000">StatefulRedisConnection</span> <span style="color:#000">connect</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">getRedisConnect</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">redisConnectionFactory</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">connect</span> <span style="color:#ce5c00;font-weight:bold">==</span> <span style="color:#204a87;font-weight:bold">null</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
        <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#204a87;font-weight:bold">null</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000;font-weight:bold">}</span>

    <span style="color:#000">CacheFrontend</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#204a87">String</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87">String</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">frontend</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">ClientSideCaching</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">enable</span><span style="color:#000;font-weight:bold">(</span>
            <span style="color:#000">CacheAccessor</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">forMap</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">new</span> <span style="color:#000">ConcurrentHashMap</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">,</span>
            <span style="color:#000">connect</span><span style="color:#000;font-weight:bold">,</span>
            <span style="color:#000">TrackingArgs</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Builder</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">enabled</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>

    <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">frontend</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#000;font-weight:bold">}</span>

<span style="color:#204a87;font-weight:bold">private</span> <span style="color:#000">StatefulRedisConnection</span> <span style="color:#000">getRedisConnect</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">RedisConnectionFactory</span> <span style="color:#000">redisConnectionFactory</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
    <span style="color:#204a87;font-weight:bold">if</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">redisConnectionFactory</span> <span style="color:#204a87;font-weight:bold">instanceof</span> <span style="color:#000">LettuceConnectionFactory</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
        <span style="color:#000">AbstractRedisClient</span> <span style="color:#000">absClient</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">LettuceConnectionFactory</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000">redisConnectionFactory</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">getNativeClient</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">absClient</span> <span style="color:#204a87;font-weight:bold">instanceof</span> <span style="color:#000">RedisClient</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
            <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">RedisClient</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000">absClient</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">connect</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000;font-weight:bold">}</span>
    <span style="color:#000;font-weight:bold">}</span>
    <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#204a87;font-weight:bold">null</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#000;font-weight:bold">}</span>
</code></pre></div>
<p>其实也简单，通过RedisConnectionFactory获取一个StatefulRedisConnection连接，就可以创建CacheFrontend了。
这里RedisClient#connect方法会创建一个新的连接，这样可以将使用客户端缓存、不使用客户端缓存的连接区分。</p>

<h3 id="结合guava缓存">结合Guava缓存</h3>

<p>Lettuce的StatefulRedisConnection类还提供了addListener方法，可以设置回调方法处理Redis推送的消息。
利用该方法，我们可以将Guava的缓存与Redis客户端缓存结合</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-typescript" data-lang="typescript"><span style="color:#204a87;font-weight:bold">@Bean</span>
<span style="color:#204a87;font-weight:bold">public</span> <span style="color:#000">LoadingCache</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#204a87">String</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87">String</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">redisGuavaCache</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">RedisConnectionFactory</span> <span style="color:#000">redisConnectionFactory</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
    <span style="color:#8f5902;font-style:italic">// [1]
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#000">StatefulRedisConnection</span> <span style="color:#000">connect</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">getRedisConnect</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">redisConnectionFactory</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">connect</span> <span style="color:#ce5c00;font-weight:bold">!=</span> <span style="color:#204a87;font-weight:bold">null</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
        <span style="color:#8f5902;font-style:italic">// [2]
</span><span style="color:#8f5902;font-style:italic"></span>        <span style="color:#000">LoadingCache</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#204a87">String</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87">String</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">redisCache</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">CacheBuilder</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">newBuilder</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span>
                <span style="color:#000;font-weight:bold">.</span><span style="color:#000">initialCapacity</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">5</span><span style="color:#000;font-weight:bold">)</span>
                <span style="color:#000;font-weight:bold">.</span><span style="color:#000">maximumSize</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">100</span><span style="color:#000;font-weight:bold">)</span>
                <span style="color:#000;font-weight:bold">.</span><span style="color:#000">expireAfterWrite</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">5</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">TimeUnit</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">MINUTES</span><span style="color:#000;font-weight:bold">)</span>
                <span style="color:#000;font-weight:bold">.</span><span style="color:#000">build</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">new</span> <span style="color:#000">CacheLoader</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#204a87">String</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87">String</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
                    <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87">String</span> <span style="color:#000">load</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87">String</span> <span style="color:#000">key</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span> 
                        <span style="color:#204a87">String</span> <span style="color:#000">val</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#204a87">String</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000">connect</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">sync</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">get</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">key</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
                        <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">val</span> <span style="color:#ce5c00;font-weight:bold">==</span> <span style="color:#204a87;font-weight:bold">null</span> <span style="color:#ce5c00;font-weight:bold">?</span> <span style="color:#4e9a06">&#34;&#34;</span> <span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#000">val</span><span style="color:#000;font-weight:bold">;</span>
                    <span style="color:#000;font-weight:bold">}</span>
                <span style="color:#000;font-weight:bold">}</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#8f5902;font-style:italic">// [3]
</span><span style="color:#8f5902;font-style:italic"></span>        <span style="color:#000">connect</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">sync</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">clientTracking</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">TrackingArgs</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Builder</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">enabled</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#8f5902;font-style:italic">// [4]
</span><span style="color:#8f5902;font-style:italic"></span>        <span style="color:#000">connect</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">addListener</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">message</span> <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000;font-weight:bold">{</span>
            <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">message</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">getType</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">equals</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;invalidate&#34;</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
                <span style="color:#000">List</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#204a87">Object</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">content</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">message</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">getContent</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">StringCodec</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">UTF8</span><span style="color:#ce5c00;font-weight:bold">:</span><span style="color:#ce5c00;font-weight:bold">:</span><span style="color:#000">decodeKey</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
                <span style="color:#000">List</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#204a87">String</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">keys</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">List</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#204a87">String</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000">content</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">get</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
                <span style="color:#000">keys</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">forEach</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">key</span> <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000;font-weight:bold">{</span>
                    <span style="color:#000">redisCache</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">invalidate</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">key</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
                <span style="color:#000;font-weight:bold">}</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#000;font-weight:bold">}</span>
        <span style="color:#000;font-weight:bold">}</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">redisCache</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000;font-weight:bold">}</span>
    <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#204a87;font-weight:bold">null</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#000;font-weight:bold">}</span>
</code></pre></div>
<ol>
<li>获取Redis连接。</li>
<li>创建Guava缓存类LoadingCache，该缓存类如果发现数据不存在，则查询Redis。</li>
<li>开启Redis客户端缓存。</li>
<li>添加回调函数，如果收到Redis发送的失效消息，则清除Guava缓存。</li>
</ol>

<h3 id="redis-cluster模式">Redis Cluster模式</h3>

<p>上面说的应用必须在Redis单机模式下（或者主从、Sentinel模式），遗憾的是，
目前发现Lettuce（6.1.5版本）还没有支持Redis Cluster下的客户端缓存，
简单看了一下源码，目前发现如下原因：
Cluster模式下，Redis命令需要根据命令的键，重定向到键的存储节点执行。
而对于“CLIENT TRACKING”这个没有键的命令，Lettuce并没有将它发送给Cluster中所有的节点，而是将它发送给一个固定的默认的节点（可查看ClusterDistributionChannelWriter类），所以通过StatefulRedisClusterConnection调用RedisAdvancedClusterCommands.clientTracking方法并没有开启Redis服务的Tracking机制。
这个其实也可以修改，有时间再研究一下。</p>

<h3 id="需要注意的问题">需要注意的问题</h3>

<p>那么单机模式下，Lettuce的客户端缓存就真的没有问题了吗？</p>

<p>仔细思考一下Redis Tracking的设计，发现使用Redis客户端缓存有两个点需要关注：</p>

<ol>
<li>开启客户端缓存后，Redis连接不能断开。
如果Redis连接断了，并且客户端自动重连，那么新的连接是没有开启Tracking机制的，该连接查询的键不会受到失效消息，后果很严重。
同样，开启Tracking的连接和查询缓存键的连接必须是同一个，不能使用A连接开启Tracking机制，使用B连接去查询缓存键（所以客户端不能使用连接池）。</li>
</ol>

<p>Redis服务器可以设置timeout配置，自动超过该配置没有发送请求的连接。
而Lettuce有自动重连机制，重连后的连接将收不到失效消息。
有两个解决思路：
（1）实现Lettuce心跳机制，定时发送PING命令以维持连接。
（2）即使使用心跳机制，Redis连接依然可能断开（网络跳动等原因），可以修改自动重连机制（ReconnectionHandler），增加如下逻辑：如果连接原来开启了Tracking机制，则重连后需要自动开启Tracking机制。
需要注意，如果使用的是非广播模式，需要清空旧连接缓存的数据，因为连接已经变更，Redis服务器不会将旧连接的失效消息发送给新连接。</p>

<ol>
<li>启用缓存的连接与未启动缓存的连接应该区分。
这点比较简单，上例例子中都使用RedisClient#connect方法创建一个新的连接，专用于客户端缓存。</li>
</ol>

<p>客户端缓存是一个强大的功能，需要我们去用好它。可惜当前暂时还没有完善的Java客户端支持，本书说了我的一些分析与思路，欢迎探讨。我后续会关注继续Lettuce的更新，如果Lettuce提供了完善的Redis客户端缓存支持，再更新本文。</p>

    </div>
    <footer class="post-footer">
     

     <div class="post-nav">
    <div class="post-nav-next post-nav-item">
    
        <a href="http://www.shutdown.cn/post/redis6.0%E6%96%B0%E7%89%B9%E6%80%A7-%E5%AE%A2%E6%88%B7%E7%AB%AF%E7%BC%93%E5%AD%98%E5%85%B6%E4%B8%89/" rel="next" title="">
        <i class="fa fa-chevron-left"></i> 
        </a>
    
    </div>

    <div class="post-nav-prev post-nav-item">
    
        <a href="http://www.shutdown.cn/post/redis6.0%E6%96%B0%E7%89%B9%E6%80%A7-%E5%AE%A2%E6%88%B7%E7%AB%AF%E7%BC%93%E5%AD%98%E5%85%B6%E4%BA%94-java%E4%BD%BF%E7%94%A8%E5%AE%9E%E8%B7%B5/" rel="prev" title="">
         <i class="fa fa-chevron-right"></i>
        </a>
    
    </div>
</div>
      
     
     
     






    </footer>
  </article>
</section>

          </div>
        </div>
        <div class="sidebar-toggle">
  <div class="sidebar-toggle-line-wrap">
    <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
  </div>
</div>
<aside id="sidebar" class="sidebar">
  <div class="sidebar-inner">

    <section class="site-overview sidebar-panel  sidebar-panel-active ">
      <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image"
        src="http://www.shutdown.cn/img/author.jpg"
        alt="不与天斗Domino" />
    <p class="site-author-name" itemprop="name">不与天斗Domino</p>
    <p class="site-description motion-element" itemprop="description"> 
        Programmer &amp; Architect</p>
</div>
      <nav class="site-state motion-element">
    <div class="site-state-item site-state-posts">
      <a href="http://www.shutdown.cn/post/">
        <span class="site-state-item-count">183</span>
        <span class="site-state-item-name">日志</span>
      </a>
    </div>
    <div class="site-state-item site-state-categories">    
        <a href="http://www.shutdown.cn/categories/">      
         
        <span class="site-state-item-count">15</span>
        
        <span class="site-state-item-name">分类</span>
        
        </a>
    </div>

    <div class="site-state-item site-state-tags">
        <a href="http://www.shutdown.cn/tags/">
         
        <span class="site-state-item-count">224</span>
        
        <span class="site-state-item-name">标签</span>
        </a>
    </div>
</nav>
      
      

      

      <div class="links-of-blogroll motion-element inline">
<script type="text/javascript" src="//rf.revolvermaps.com/0/0/8.js?i=&amp;m=0&amp;s=220&amp;c=ff0000&amp;cr1=ffffff&amp;f=arial&amp;l=33&amp;bv=35" async="async"></script>
</div>

    </section>
    
  </div>
</aside>

      </div>
    </main>
   
    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  <span itemprop="copyrightYear">  &copy; 
  2013 - 2023</span>
  <span class="with-love"><i class="fa fa-heart"></i></span>
  <span class="author" itemprop="copyrightHolder">天地维杰网</span>
  <span class="icp" itemprop="copyrightHolder"><a href="https://beian.miit.gov.cn/" target="_blank">京ICP备13019191号-1</a></span>
</div>
<div class="powered-by">
  Powered by - <a class="theme-link" href="http://gohugo.io" target="_blank" title="hugo" >Hugo v0.63.2</a>
</div>
<div class="theme-info">
  Theme by - <a class="theme-link" href="https://github.com/xtfly/hugo-theme-next" target="_blank"> NexT
  </a>
</div>


      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
      <span id="scrollpercent"><span>0</span>%</span>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery/index.js?v=2.1.3"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/fastclick/lib/fastclick.min.js?v=1.0.6"></script> 
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.min.js?v=1.2.1"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script src="http://www.shutdown.cn/js/vendor/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>

<script src="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.pack.js?v=2.1.5"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/utils.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/motion.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/affix.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/schemes/pisces.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/scrollspy.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/post-details.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/toc.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/bootstrap.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js"],
    jax: ["input/TeX", "output/HTML-CSS"],
    tex2jax: {
      inlineMath: [ ['$','$'] ],
      displayMath: [ ['$$','$$'] ],
      processEscapes: true
    },
    "HTML-CSS": { fonts: ["TeX"] }
  });
</script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML' async></script>
</body>
</html>